namespace DataClasses
{
    using System;
    using System.Collections.Generic;
    using MySql.Data.MySqlClient;

    /// <summary>
    /// Model of playlist
    /// </summary>
    public class Playlist : ActiveRecord<Playlist>
    {
        private List<Song> _songs = new List<Song>(); 

        /// <summary>
        /// Empty constructor for getting entities needs
        /// </summary>
        public Playlist() { }


        /// <summary>
        /// Constructor of the Playlist
        /// </summary>
        /// <param name="name"></param>
        public Playlist(string name)
        {
            Id = Guid.NewGuid();
            Name = name;
            Save();
        }

        public Playlist(Guid self, string name, int count)
        {
            Id = self;
            Name = name;
            Count = count;
        }

        /// <summary>
        /// Name of the playlist
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Count of songs in playlist
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// List of songs
        /// </summary>
        public List<Song> Songs
        {
            get
            {
                if (_songs.Count == 0)
                {
                    _songs = GetSongs();
                }

                return _songs;
            }
        } 

        public override object[] Objparams
        {
            get
            {
                objects = new object[3];
                objects[0] = Id.ToString();
                objects[1] = Name;
                objects[2] = Count;
                return objects;
            }

            set
            {
                objects = value;
                Id = new Guid(objects[0].ToString());
                Name = (string)objects[1];
                Count = (int)objects[2];
            }
        }

        /*
         * 
         * "SELECT s.id,s.idGenre,s.idArtist,s.name,s.duration,s.path "
                    + "FROM song s JOIN playlistsong pls ON pls.idsong = s.id "
                    + "LEFT JOIN playlist pl ON pls.idplaylist = pl.id " 
                    + "WHERE pl.id=@plid;", 
         * 
         */

        /// <summary>
        /// Method for getting songs connected with current playlist
        /// </summary>
        /// <param name="id"></param>
        /// <returns>List of songs</returns>
        public List<Song> GetSongs(Guid? id = null)
        {
            if (id == null)
            {
                id = Id;
            }
            
            var songs = new List<Song>();
            var command =
                new MySqlCommand(
                    "SELECT s.id,s.idGenre,s.name,s.duration,s.path " 
                    + "FROM song s,playlistsong pls "
                    + "LEFT JOIN playlist pl ON pls.idplaylist = pl.id  " 
                    + "WHERE pls.idsong = s.id AND pl.id = @plid;",
                    Connection);
            command.Parameters.Add("@plid",MySqlDbType.Guid).Value = id.Value.ToString();
            using (Connection)
            {
                Connection.Open();
                var reader = command.ExecuteReader();
                while (reader.Read())
                {
                    var temp = new object[reader.FieldCount];
                    reader.GetValues(temp);
                    songs.Add(new Song() { Objparams = temp });
                }

                return songs;
            }
        }
    }
}
